package net.servir.capacidades.web.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import net.servir.capacidades.dao.JdbcExportarExcelDAO;
import net.servir.capacidades.model.CONSTANT;
import net.servir.capacidades.model.xls.XlsWorkbookConfig;
import net.servir.capacidades.model.xls.XlsWorksheetConfig;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ServletExportExcelPlus extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String sSQLEjecutar = "";


		WebApplicationContext springContext = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());

		DataSource ds1 = (DataSource) springContext.getBean("dataSource");
		JdbcExportarExcelDAO jdbcExportExcelDAO = new JdbcExportarExcelDAO(ds1);

		//Obtenemos la informacion para Exportar a Excel
		String key = CONSTANT.KEY_CFG_EXCEL_EXPORT;
		if (request.getParameter("key") != null && request.getParameter("key").equals(""))
			key = request.getParameter("key");
		
		HttpSession sesion = request.getSession();
		XlsWorkbookConfig xlsWorkbook = (XlsWorkbookConfig)sesion.getAttribute(key);

		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition","attachment;filename="+xlsWorkbook.getName()+".xls");
		HSSFWorkbook wb = new HSSFWorkbook(); // crea libro
		
		for (XlsWorksheetConfig xlsWorksheet : xlsWorkbook.getSheets()) {
			HSSFSheet sheet = wb.createSheet(xlsWorksheet.getName());// crea hoja			
			jdbcExportExcelDAO.FillExcelSheet(xlsWorksheet, sheet, wb);			
		}
		OutputStream out = response.getOutputStream();
		wb.write(out);
		out.close();
	}

	// <editor-fold defaultstate="collapsed"
	// desc="metodos doGet y doPost creados por NetBeans">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>
}
